package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.Wares;
import com.lin.pojo.WaresOrder;
import com.lin.services.SaleReportService;
import com.lin.services.WaresService;
import com.lin.utils.PageUnit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * ClassName SaleReportController
 * Package com.lin.controller
 *
 * @author hellcat
 * Date 2020/3/18
 * Description:进货报表管理
 * 🐾
 */
@Controller
@RequestMapping("saleReport")
public class SaleReportController {
    @Autowired
    private SaleReportService saleReportService;
    @Autowired
    private WaresService waresService;

    @MyLog("查看退货报表")
    @RequiresPermissions("report:saleReport:list")
    @RequestMapping(value = "/toSaleReport", method = RequestMethod.GET)
    public String toSaleReport(HttpServletRequest request) {
        request.setAttribute("ddId", "SaleReport");
        return "saleReportMain";
    }

    @RequiresPermissions("report:saleReport:list")
    @RequestMapping(value = "/toSaleReportDetails", method = RequestMethod.POST)
    public String toSaleReportDetails(HttpServletRequest request) {
        request.setAttribute("ddId", "SaleReport");
        String id = request.getParameter("waresId");
        String date = request.getParameter("date");
        request.setAttribute("searchDate", date);
        Wares wares = waresService.getCategoryById(id);
        WaresOrder order = saleReportService.getWaresById(id, date);
        request.setAttribute("wares", wares);
        request.setAttribute("order", order);
        return "saleReportDetails";

    }

    @ResponseBody
    @RequiresPermissions("report:saleReport:list")
    @RequestMapping(value = "/getSaleReportDetails", method = RequestMethod.POST)
    public Map<String, Object> getSaleReportDetails(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String searchDate = request.getParameter("searchDate");
        String searchWaresId = request.getParameter("searchWaresId");
        List<WaresOrder> list = saleReportService.getSaleReportDetails(searchDate, searchWaresId);
        return PageUnit.Page(list, page, limit);
    }

    @ResponseBody
    @RequiresPermissions("report:saleReport:list")
    @RequestMapping(value = "/getSaleReport", method = RequestMethod.POST)
    public Map<String, Object> getSaleReport(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String searchDate = request.getParameter("searchDate");
        String searchWaresId = request.getParameter("searchWaresId");
        List<WaresOrder> list = saleReportService.getSaleReport(searchDate, searchWaresId);
        return PageUnit.Page(list, page, limit);
    }

    @ResponseBody
    @RequiresPermissions("report:saleReport:list")
    @RequestMapping(value = "/getChartsData", method = RequestMethod.POST)
    public Map<String, Object> getChartsData(HttpServletRequest request) {
        String date = request.getParameter("date");
        return saleReportService.getChartsData(date);
    }
}
